package com.example.dollarShipping.controller.account;


//import com.example.dollarShipping.entity.ShippingOrder;
//import com.example.dollarShipping.service.AccountService;
//import com.example.dollarShipping.utils.Result;
//import com.example.dollarShipping.utils.ResultCode;
//import org.springframework.security.access.prepost.PreAuthorize;
//import org.springframework.validation.annotation.Validated;
import com.example.dollarShipping.entity.Carrier;
import com.example.dollarShipping.entity.Menu;
import com.example.dollarShipping.entity.ShippingOrder;
import com.example.dollarShipping.security.UserDetailsImpl;
import com.example.dollarShipping.service.AccountService;
import com.example.dollarShipping.service.CarrierService;
import com.example.dollarShipping.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/account")
public class AccountController {

    @Resource
    AccountService accountService;
    @Resource
    CarrierService carrierService;
    @Resource
    private MenuService menuService;

    @ModelAttribute("menuList")
    public List<Menu> backGround() {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<Menu>  menu= menuService.getMenu(userDetails.getRoleId());
        return menu;
    }


    @RequestMapping("/createPage")
    public String showCreatePage(Model model) {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        ShippingOrder shippingOrder = new ShippingOrder();
        model.addAttribute("driverTruckOrders",shippingOrder);
        model.addAttribute("userId",userDetails.getId());
        System.out.println("createPage");
        return "account/createShippingOrder";
    }

    @RequestMapping("/myShippingOrder")
    public String showShippingOrderList(Model model) {
        UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        List<ShippingOrder> sl = accountService.getShippingOrder(userDetails.getId());
        model.addAttribute("shippingOrderList",sl);
        model.addAttribute("userId",userDetails.getId());
        System.out.println("getShippingOrderList");
        return "account/myShippingList";
    }

    @GetMapping("detail{orderId}")
    public String getCarrierList(Model model, @PathVariable(name="orderId") Long orderId){
        ShippingOrder s = carrierService.findShippingOrder(orderId);
        model.addAttribute("shippingOrder",s);
        return "detailShippingOrder";
    }

    // 货运单签收/结算操作
    // 修改货运单状态并添加任务记录
    @PostMapping("/accountOperate")
    public String accountOperate(@RequestParam Map<String,Object> params){
        System.out.println("accountOperate");
        Integer state = Integer.parseInt(params.get("state").toString());
        Long id = Long.parseLong(params.get("id").toString());
        Long operator = Long.parseLong(params.get("operator").toString());
        try {
            accountService.changeState(state, id, operator);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "driver/driverTruckOrder";
    }

    //创建货运单
    @PostMapping("createShippingOrder")
    public String createShippingOrder(@RequestParam Map<String,Object> params){
        System.out.println("createShippingOrder");
        Long creatorId = Long.parseLong(params.get("creatorId").toString());
        String start = params.get("start").toString();
        String end = params.get("target").toString();
        String consignee = params.get("consignee").toString();
        Float distance = Float.parseFloat(params.get("distance").toString());
        String goods = params.get("goods").toString();
        Float weight = Float.parseFloat(params.get("weight").toString());
        String desc = params.get("desc").toString();
        try {
            accountService.createShippingOrder(creatorId,start,end,consignee,distance,goods,weight,desc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "driver/driverTruckOrder";
    }


}
